package cn.sunny360.messsage.impl.json;

import cn.sunny360.messsage.MsgRequest;
import com.alibaba.fastjson.JSONObject;

import java.nio.charset.StandardCharsets;

public class JsonMsgRequest implements MsgRequest<JsonMsgRequest> {
    private JSONObject content;

    public JsonMsgRequest(){
        content = new JSONObject();
    }

    public JsonMsgRequest(String json){
        content = JSONObject.parseObject(json);
    }

    public JsonMsgRequest(JSONObject jsonObject) {
        content = jsonObject;
    }

    @Override
    public String toJson() {
        return content.toJSONString();
    }

    @Override
    public JsonMsgRequest parseJson(String s) {
        content = JSONObject.parseObject(s);
        return this;
    }

    @Override
    public byte[] toByteArray() {
        return content.toJSONString().getBytes(StandardCharsets.UTF_8);
    }

    @Override
    public JsonMsgRequest parseByteArray(byte[] bytes) {
        content = JSONObject.parseObject(new String(bytes, StandardCharsets.UTF_8));
        return this;
    }

    @Override
    public int getType() {
        return content.getInteger("type");
    }

    @Override
    public String getUserId() {
        return content.getString("user_id");
    }

    @Override
    public String getUserName() {
        return content.getString("username");
    }

    @Override
    public String getPassword() {
        return content.getString("password");
    }

    @Override
    public String getToken() {
        return content.getString("token");
    }

    @Override
    public String getMessage() {
        return content.getString("message");
    }

    @Override
    public String getLastVersion() {
        return content.getString("last_version");
    }

    @Override
    public String getExt() {
        return content.getString("ext");
    }

    @Override
    public MsgRequest setType(int type) {
        content.put("type", type);
        return this;
    }

    @Override
    public MsgRequest setUserId(String userId) {
        content.put("user_id", userId);
        return this;
    }

    @Override
    public MsgRequest setUserName(String userName) {
        content.put("username", userName);
        return this;
    }

    @Override
    public MsgRequest setPassword(String password) {
        content.put("password", password);
        return this;
    }

    @Override
    public MsgRequest setToken(String token) {
        content.put("token", token);
        return this;
    }

    @Override
    public MsgRequest setMessage(String message) {
        content.put("message", message);
        return this;
    }

    @Override
    public MsgRequest setLastVersion(String lastVersion) {
        content.put("last_version", lastVersion);
        return this;
    }

    @Override
    public MsgRequest setExt(String ext) {
        content.put("ext", ext);
        return this;
    }


}
